package com.bw.base.imageLoader.impl

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bw.base.imageLoader.IImageLoader
import com.bw.base.imageLoader.ImageOptions
import java.io.File


class GlideStrategy : IImageLoader {
    override fun loadFileIntoImageView(
        context: Context?,
        url: String?,
        imageView: ImageView?,
        options: ImageOptions?
    ) {
        if (context != null) {
            if (url != null) {
                if (imageView != null) {
                    loadImageView(context,url,imageView,options)
                }
            }
        };

    }

    override fun loadFileIntoImageView(
        context: Context?,
        content: ByteArray?,
        imageView: ImageView?,
        options: ImageOptions?
    ) {
        TODO("Not yet implemented")
    }

    override fun loadFileIntoImageView(
        context: Context?,
        resId: Int,
        imageView: ImageView?,
        options: ImageOptions?
    ) {
        TODO("Not yet implemented")
    }

    override fun loadFileIntoImageView(
        context: Context?,
        file: File?,
        imageView: ImageView?,
        options: ImageOptions?
    ) {
        TODO("Not yet implemented")
    }

    private fun loadImageView(
        context: Context,
        content: Any,
        imageView: ImageView,
        options: ImageOptions?
    ) {
        val requestManager: RequestManager = Glide.with(context)
        val requestOptions = RequestOptions()
        if (options != null) {
            if (options.placeImage > 0) {
                requestOptions.placeholder(options.placeImage)
            }
            if (options.errorImage > 0) {
                requestOptions.error(options.errorImage)
            }
            if (options.skipMemoryCache) {
                requestOptions.skipMemoryCache(true)
            }
            if (options.skipDiskCache) {
                requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
            }
        }
        //        requestManager.setDefaultRequestOptions(requestOptions);
        requestManager.load(content)
            .apply(requestOptions)
            .into(imageView)
    }


}